<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>mockcontrol.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_mockcontrol.js.html">mockcontrol.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A MockControl holds a set of mocks for a particular test.
<a name="line17"></a> * It consolidates calls to $replay, $verify, and $tearDown, which simplifies
<a name="line18"></a> * the test and helps avoid omissions.
<a name="line19"></a> *
<a name="line20"></a> * You can create and control a mock:
<a name="line21"></a> *   var mockFoo = mockControl.addMock(new MyMock(Foo));
<a name="line22"></a> *
<a name="line23"></a> * MockControl also exposes some convenience functions that create
<a name="line24"></a> * controlled mocks for common mocks: StrictMock, LooseMock,
<a name="line25"></a> * FunctionMock, MethodMock, and GlobalFunctionMock.
<a name="line26"></a> *
<a name="line27"></a> */
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>goog.provide(&#39;goog.testing.MockControl&#39;);
<a name="line31"></a>
<a name="line32"></a>goog.require(&#39;goog.array&#39;);
<a name="line33"></a>goog.require(&#39;goog.testing&#39;);
<a name="line34"></a>goog.require(&#39;goog.testing.LooseMock&#39;);
<a name="line35"></a>goog.require(&#39;goog.testing.MockInterface&#39;);
<a name="line36"></a>goog.require(&#39;goog.testing.StrictMock&#39;);
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>
<a name="line40"></a>/**
<a name="line41"></a> * Controls a set of mocks.  Controlled mocks are replayed, verified, and
<a name="line42"></a> * cleaned-up at the same time.
<a name="line43"></a> * @constructor
<a name="line44"></a> */
<a name="line45"></a>goog.testing.MockControl = function() {
<a name="line46"></a>  /**
<a name="line47"></a>   * The list of mocks being controlled.
<a name="line48"></a>   * @type {Array.&lt;goog.testing.MockInterface&gt;}
<a name="line49"></a>   * @private
<a name="line50"></a>   */
<a name="line51"></a>  this.mocks_ = [];
<a name="line52"></a>};
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Takes control of this mock.
<a name="line57"></a> * @param {goog.testing.MockInterface} mock Mock to be controlled.
<a name="line58"></a> * @return {goog.testing.MockInterface} The same mock passed in,
<a name="line59"></a> *     for convenience.
<a name="line60"></a> */
<a name="line61"></a>goog.testing.MockControl.prototype.addMock = function(mock) {
<a name="line62"></a>  this.mocks_.push(mock);
<a name="line63"></a>  return mock;
<a name="line64"></a>};
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Calls replay on each controlled mock.
<a name="line69"></a> */
<a name="line70"></a>goog.testing.MockControl.prototype.$replayAll = function() {
<a name="line71"></a>  goog.array.forEach(this.mocks_, function(m) {
<a name="line72"></a>    m.$replay();
<a name="line73"></a>  });
<a name="line74"></a>};
<a name="line75"></a>
<a name="line76"></a>
<a name="line77"></a>/**
<a name="line78"></a> * Calls reset on each controlled mock.
<a name="line79"></a> */
<a name="line80"></a>goog.testing.MockControl.prototype.$resetAll = function() {
<a name="line81"></a>  goog.array.forEach(this.mocks_, function(m) {
<a name="line82"></a>    m.$reset();
<a name="line83"></a>  });
<a name="line84"></a>};
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Calls verify on each controlled mock.
<a name="line89"></a> */
<a name="line90"></a>goog.testing.MockControl.prototype.$verifyAll = function() {
<a name="line91"></a>  goog.array.forEach(this.mocks_, function(m) {
<a name="line92"></a>    m.$verify();
<a name="line93"></a>  });
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Calls tearDown on each controlled mock, if necesssary.
<a name="line99"></a> */
<a name="line100"></a>goog.testing.MockControl.prototype.$tearDown = function() {
<a name="line101"></a>  goog.array.forEach(this.mocks_, function(m) {
<a name="line102"></a>    // $tearDown if defined.
<a name="line103"></a>    if (m.$tearDown) {
<a name="line104"></a>      m.$tearDown();
<a name="line105"></a>    }
<a name="line106"></a>    // TODO(user): Somehow determine if verifyAll should have been called
<a name="line107"></a>    // but was not.
<a name="line108"></a>  });
<a name="line109"></a>};
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Creates a controlled StrictMock.  Passes its arguments through to the
<a name="line114"></a> * StrictMock constructor.
<a name="line115"></a> * @param {Object} objectToMock The object to mock.
<a name="line116"></a> * @param {boolean=} opt_mockStaticMethods An optional argument denoting that
<a name="line117"></a> *     a mock should be constructed from the static functions of a class.
<a name="line118"></a> * @param {boolean=} opt_createProxy An optional argument denoting that
<a name="line119"></a> *     a proxy for the target mock should be created.
<a name="line120"></a> * @return {goog.testing.StrictMock} The mock object.
<a name="line121"></a> */
<a name="line122"></a>goog.testing.MockControl.prototype.createStrictMock = function(
<a name="line123"></a>    objectToMock, opt_mockStaticMethods, opt_createProxy) {
<a name="line124"></a>  var m = new goog.testing.StrictMock(objectToMock, opt_mockStaticMethods,
<a name="line125"></a>                                      opt_createProxy);
<a name="line126"></a>  this.addMock(m);
<a name="line127"></a>  return m;
<a name="line128"></a>};
<a name="line129"></a>
<a name="line130"></a>
<a name="line131"></a>/**
<a name="line132"></a> * Creates a controlled LooseMock.  Passes its arguments through to the
<a name="line133"></a> * LooseMock constructor.
<a name="line134"></a> * @param {Object} objectToMock The object to mock.
<a name="line135"></a> * @param {boolean=} opt_ignoreUnexpectedCalls Whether to ignore unexpected
<a name="line136"></a> *     calls.
<a name="line137"></a> * @param {boolean=} opt_mockStaticMethods An optional argument denoting that
<a name="line138"></a> *     a mock should be constructed from the static functions of a class.
<a name="line139"></a> * @param {boolean=} opt_createProxy An optional argument denoting that
<a name="line140"></a> *     a proxy for the target mock should be created.
<a name="line141"></a> * @return {goog.testing.LooseMock} The mock object.
<a name="line142"></a> */
<a name="line143"></a>goog.testing.MockControl.prototype.createLooseMock = function(
<a name="line144"></a>    objectToMock, opt_ignoreUnexpectedCalls,
<a name="line145"></a>    opt_mockStaticMethods, opt_createProxy) {
<a name="line146"></a>  var m = new goog.testing.LooseMock(objectToMock, opt_ignoreUnexpectedCalls,
<a name="line147"></a>                                     opt_mockStaticMethods, opt_createProxy);
<a name="line148"></a>  this.addMock(m);
<a name="line149"></a>  return m;
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * Creates a controlled FunctionMock.  Passes its arguments through to the
<a name="line155"></a> * FunctionMock constructor.
<a name="line156"></a> * @param {string=} opt_functionName The optional name of the function to mock
<a name="line157"></a> *     set to &#39;[anonymous mocked function]&#39; if not passed in.
<a name="line158"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line159"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line160"></a> * @return {goog.testing.MockInterface} The mocked function.
<a name="line161"></a> */
<a name="line162"></a>goog.testing.MockControl.prototype.createFunctionMock = function(
<a name="line163"></a>    opt_functionName, opt_strictness) {
<a name="line164"></a>  var m = goog.testing.createFunctionMock(opt_functionName, opt_strictness);
<a name="line165"></a>  this.addMock(m);
<a name="line166"></a>  return m;
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Creates a controlled MethodMock.  Passes its arguments through to the
<a name="line172"></a> * MethodMock constructor.
<a name="line173"></a> * @param {Object} scope The scope of the method to be mocked out.
<a name="line174"></a> * @param {string} functionName The name of the function we&#39;re going to mock.
<a name="line175"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line176"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line177"></a> * @return {goog.testing.MockInterface} The mocked method.
<a name="line178"></a> */
<a name="line179"></a>goog.testing.MockControl.prototype.createMethodMock = function(
<a name="line180"></a>    scope, functionName, opt_strictness) {
<a name="line181"></a>  var m = goog.testing.createMethodMock(scope, functionName, opt_strictness);
<a name="line182"></a>  this.addMock(m);
<a name="line183"></a>  return m;
<a name="line184"></a>};
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>/**
<a name="line188"></a> * Creates a controlled MethodMock for a constructor.  Passes its arguments
<a name="line189"></a> * through to the MethodMock constructor. See
<a name="line190"></a> * {@link goog.testing.createConstructorMock} for details.
<a name="line191"></a> * @param {Object} scope The scope of the constructor to be mocked out.
<a name="line192"></a> * @param {string} constructorName The name of the function we&#39;re going to mock.
<a name="line193"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line194"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line195"></a> * @return {goog.testing.MockInterface} The mocked method.
<a name="line196"></a> */
<a name="line197"></a>goog.testing.MockControl.prototype.createConstructorMock = function(
<a name="line198"></a>    scope, constructorName, opt_strictness) {
<a name="line199"></a>  var m = goog.testing.createConstructorMock(scope, constructorName,
<a name="line200"></a>                                             opt_strictness);
<a name="line201"></a>  this.addMock(m);
<a name="line202"></a>  return m;
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Creates a controlled GlobalFunctionMock.  Passes its arguments through to the
<a name="line208"></a> * GlobalFunctionMock constructor.
<a name="line209"></a> * @param {string} functionName The name of the function we&#39;re going to mock.
<a name="line210"></a> * @param {number=} opt_strictness One of goog.testing.Mock.LOOSE or
<a name="line211"></a> *     goog.testing.Mock.STRICT. The default is STRICT.
<a name="line212"></a> * @return {goog.testing.MockInterface} The mocked function.
<a name="line213"></a> */
<a name="line214"></a>goog.testing.MockControl.prototype.createGlobalFunctionMock = function(
<a name="line215"></a>    functionName, opt_strictness) {
<a name="line216"></a>  var m = goog.testing.createGlobalFunctionMock(functionName, opt_strictness);
<a name="line217"></a>  this.addMock(m);
<a name="line218"></a>  return m;
<a name="line219"></a>};
</pre>


</body>
</html>
